package com.tn.controller.usermanagement;

import com.tn.controller.alarm.common.enums.CommonCodeEnum;
import com.tn.controller.alarm.common.model.CommonVO;
import com.tn.controller.alarm.common.model.PageInput;
import com.tn.controller.alarm.common.model.PageResult;
import com.tn.controller.alarm.common.model.user.entity.DeptDO;
import com.tn.controller.alarm.common.model.user.entity.Menu;
import com.tn.controller.alarm.common.model.user.vo.MenuUserVo;
import com.tn.controller.alarm.common.model.user.vo.UserVO;
import com.tn.exception.DatabaseOperationFailedException;
import com.tn.exception.InvalidParamException;
import com.tn.service.usermanagement.IUserManagementService;
import com.tn.utils.CheckParamUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 人员管理controller
 *
 * @program: tn_aucy_java
 * @author: yangjiayao
 * @create: 2021/12/24
 */
@Slf4j
@RestController
@RequestMapping("/aucy")
public class UserManagementController {
    /**
     * service
     */
    private IUserManagementService userManagementService;

    /**
     * 注入
     *
     * @param userManagementService userManagementService
     */
    @Autowired
    public void setUserManagementService(IUserManagementService userManagementService) {
        this.userManagementService = userManagementService;
    }

    /**
     * 新增部门
     *
     * @param dept 部门信息
     * @return 结果
     * @throws DatabaseOperationFailedException 异常
     * @throws InvalidParamException            异常
     */
    @RequestMapping("/addDept")
    CommonVO<Object> addDept(@RequestBody DeptDO dept)
            throws DatabaseOperationFailedException, InvalidParamException {
        CheckParamUtil.checkBlankRequiredParam(dept.getDeptName());
        return userManagementService.addDept(dept);
    }

    /**
     * 删除部门
     *
     * @param dept 部门信息
     * @return 结果
     * @throws DatabaseOperationFailedException 异常
     * @throws InvalidParamException            异常
     */
    @RequestMapping("/deleteDept")
    CommonVO<Object> deleteDept(@RequestBody DeptDO dept)
            throws DatabaseOperationFailedException, InvalidParamException {
        CheckParamUtil.checkNullRequiredParam(dept.getId());
        return userManagementService.deleteDept(dept);
    }

    /**
     * 修改部门
     *
     * @param dept 部门信息
     * @return 结果
     * @throws DatabaseOperationFailedException 异常
     * @throws InvalidParamException            异常
     */
    @RequestMapping("/modifyDept")
    CommonVO<Object> modifyDept(@RequestBody DeptDO dept)
            throws DatabaseOperationFailedException, InvalidParamException {
        CheckParamUtil.checkNullRequiredParam(dept.getId());
        CheckParamUtil.checkBlankRequiredParam(dept.getDeptName());
        return userManagementService.modifyDept(dept);
    }

    /**
     * 查询部门
     *
     * @param pageDept 部门信息
     * @return 结果
     * @throws InvalidParamException 异常
     */
    @RequestMapping("/queryDept")
    CommonVO<PageResult<DeptDO>> queryDept(@RequestBody PageInput<DeptDO> pageDept)
            throws InvalidParamException {
        CheckParamUtil.checkPageParam(pageDept.getCurPage(), pageDept.getPageSize());
        return userManagementService.queryDept(pageDept);
    }

    /**
     * 新增用户
     *
     * @param user 用户信息
     * @return 结果
     * @throws DatabaseOperationFailedException 异常
     * @throws InvalidParamException            异常
     */
    @RequestMapping("/addUser")
    CommonVO<Object> addUser(@RequestBody UserVO user)
            throws DatabaseOperationFailedException, InvalidParamException {
        CheckParamUtil.checkNullRequiredParam(user.getDeptId());
        CheckParamUtil.checkBlankRequiredParam(user.getUserName(), user.getPassword());
        return userManagementService.addUser(user);
    }

    /**
     * 新增用户
     *
     * @param user 用户信息
     * @return 结果
     * @throws DatabaseOperationFailedException 异常
     * @throws InvalidParamException            异常
     */
    @RequestMapping("/deleteUser")
    CommonVO<Object> deleteUser(@RequestBody UserVO user)
            throws DatabaseOperationFailedException, InvalidParamException {
        CheckParamUtil.checkNullRequiredParam(user.getUserId());
        return userManagementService.deleteUser(user);
    }

    /**
     * 新增用户
     *
     * @param user 用户信息
     * @return 结果
     * @throws DatabaseOperationFailedException 异常
     * @throws InvalidParamException            异常
     */
    @RequestMapping("/modifyUser")
    CommonVO<Object> modifyUser(@RequestBody UserVO user)
            throws DatabaseOperationFailedException, InvalidParamException {
        CheckParamUtil.checkNullRequiredParam(user.getUserId());
        if (StringUtils.isBlank(user.getPassword())) {
            if (StringUtils.isBlank(user.getUserName()) || null == user.getDeptId()) {
                throw new InvalidParamException(CommonCodeEnum.PARAM_MISSING.getCode(),
                        CommonCodeEnum.PARAM_MISSING.getMessage());
            }
        } else {
            if (StringUtils.isNotBlank(user.getUserName()) || null != user.getDeptId()) {
                throw new InvalidParamException(CommonCodeEnum.PARAM_CONFLICT.getCode(),
                        CommonCodeEnum.PARAM_CONFLICT.getMessage());
            }
        }
        return userManagementService.modifyUser(user);
    }

    /**
     * 新增用户
     *
     * @param pageUser 用户信息
     * @return 结果
     * @throws InvalidParamException 异常
     */
    @RequestMapping("/queryUser")
    CommonVO<PageResult<UserVO>> queryUser(@RequestBody PageInput<UserVO> pageUser)
            throws InvalidParamException {
        CheckParamUtil.checkPageParam(pageUser.getCurPage(), pageUser.getPageSize());
        CheckParamUtil.checkNullRequiredParam(pageUser.getData());
        CheckParamUtil.checkNullRequiredParam(pageUser.getData().getIsSuperuser());
        CheckParamUtil.checkBlankRequiredParam(pageUser.getData().getOperateUser());
        return userManagementService.queryUser(pageUser);
    }

    /**
     * 添加到超级用户
     *
     * @param user 用户信息
     * @return 结果
     * @throws InvalidParamException 异常
     */
    @RequestMapping("/modifySuperUser")
    CommonVO<Object> queryUser(@RequestBody UserVO user) throws InvalidParamException {
        CheckParamUtil.checkNullRequiredParam(user.getUserId(), user.getIsSuperuser());
        return userManagementService.modifySuperUser(user);
    }


    /**
     * 新增菜单
     *
     * @param menu 用户信息
     * @return 结果
     * @throws InvalidParamException 异常
     */
    @RequestMapping("/addMenu")
    CommonVO<Object> addMenu(@RequestBody Menu menu)  {
        return userManagementService.addMenu(menu);
    }

    /**
     * 编辑菜单
     *
     * @param menu 用户信息
     * @return 结果
     * @throws InvalidParamException 异常
     */
    @RequestMapping("/editMenu")
    CommonVO<Object> editMenu(@RequestBody Menu menu) {
        return userManagementService.editMenu(menu);
    }


    /**
     * 删除菜单
     *
     * @param menu 用户信息
     * @return 结果
     * @throws InvalidParamException 异常
     */
    @GetMapping("/deleteMenu/{id}")
    CommonVO<Object> deleteMenu(@PathVariable("id")Long id)  {
        return userManagementService.deleteMenu(id);
    }

    /**
     * 查询菜单
     *
     * @param menu 用户信息
     * @return 结果
     * @throws InvalidParamException 异常
     */
    @GetMapping("/selectAllMenu/{id}")
    CommonVO<Object> selectAllMenu(@PathVariable("id")Long id)  {
        return userManagementService.selectAllMenu(id);
    }

    /**
     * 绑定用户和菜单的关系
     *
     * @param menu 用户信息
     * @return 结果
     * @throws InvalidParamException 异常
     */
    @RequestMapping("/bindUserMenu")
    CommonVO<Object> bindUserMenu(@RequestBody MenuUserVo vo)  {
        return userManagementService.bindUserMenu(vo);
    }


}
